<?php
namespace app\finance\controller;
use think\Request;
use think\Exception;
use app\common\controller\Common;
class Income extends Common
{
	/**
	* 收入记账列表
	*/
	public function index()
	{
		if(Request()->isGet()){
			/*@门店信息*/
			$shop = model('commin/Shop')->select();
			$this->assign('shop',$shop);
			return $this->fetch('');
		}
		if(Request()->isAjax()){
			$data = Request()->param('');
			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			$where = $this->handleIndex($data);

			$res['list'] = model('Income')->incomeList($where['order'], $where['repair'], $page, $row, $where['ty']);
			$count = model('Income')->incomeCount($where['order'], $where['repair'], $where['ty']);
			$res['count'] = model('Income')->incomeSum($where['order'], $where['repair'], $where['ty']);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $res, $page, $row);
		}
	}
	/******************* 私有 *****************/
	/**
	* 收入记账列表-条件处理
	*/
	private function handleIndex($data)
	{
		$where = [];
		$where['order'] = [];
		$where['repair'] = [];
		$where['ty'] = '';
		/*@门店*/
		if(session('admin.admin_type') == 0){/*总部*/
			if(isset($data['shop_id']) && $data['shop_id'] != ''){
				$where['order']['o.shop_id'] = $data['shop_id'];
				$where['repair']['r.shop_id'] = $data['shop_id'];
			}
		}else{
			$where['order']['o.shop_id'] = session('admin.shop_id');
			$where['repair']['r.shop_id'] = session('admin.shop_id');
		}
		
		/*@支付状态*/
		if(isset($data['state']) && $data['state'] != ''){
			$where['order']['o.state'] = $data['state'];
			$where['repair']['r.pay_state'] = $data['state'];
		}
		/*@时间*/
		if($cusTime = $this->cusTime($data)){
			$where['order']['o.addtime'] = $where['repair']['r.addtime'] = $cusTime;
		}
		/*@类型*/
		if(isset($data['ty']) && $data['ty'] != ''){
			$where['ty'] = $data['ty'];
		}
		return $where;
	}
}